//
//  CailiaoViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CailiaoViewController.h"
@interface CailiaoViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation CailiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:234 / 255.0 green:86 / 255.0 blue:81 / 255.0 alpha:1.0];
    self.navigationItem.title = @"课程";
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[ClassTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
}
- (void)dataHandler
{
    [AFNModel GETWithURL:@"http://yuntuapi.amap.com/datasearch/local?tableid=550fa56ce4b050797967f047&city=%E5%85%A8%E5%9B%BD&keywords=&filter=%2Bcity_name%3A%E5%A4%A7%E8%BF%9E%2B&sortrule=_createtime%3A0&limit=20&page=1&key=55f0eadf1e00f8defca60c0f4bb1e700&ts=1453691318415&scode=aa4ab60a11bba69b04ed31f36e2c9dd8" completion:^(id result) {
        NSArray *arr = result[@"datas"];
        self.dataArr = [ClassModel modelHandlerWithArray:arr];
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH / 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ClassModel *m = _dataArr[indexPath.row];
    cell.titleLabel.text = m.subject;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.class_image]];
    if ([m.is_free integerValue] == 1) {
        cell.freeView.image = [UIImage imageNamed:@"free"];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClassModel *m = _dataArr[indexPath.row];
    ClassDetialViewController *classDetialVC = [[ClassDetialViewController alloc] init];
    classDetialVC.cid = m.cid;
    [self.navigationController pushViewController:classDetialVC animated:YES];
    [classDetialVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
